<template>
	<main :style="bottomPadding">
		<van-list v-if="goodsList&&goodsList.length>0"
				  :finished="finished" v-model:loading="loading" v-model:error="error" error-text="请求失败，点击重新加载">
			<template #finished><van-divider>没有更多了</van-divider></template>
			<van-row :gutter="8">
				<GoodsFavoritesItem v-for="item in goodsList" :item="item" key="item.gdsId" :show-mask="operSwitch" @check-change="checkDentity"/>
			</van-row>
		</van-list>
		<van-empty v-else image="search" description="您暂无收藏的商品~" />
	</main>
</template>
<style lang="scss" scoped>
.van-row{margin:0 8px}
</style>
<script lang="ts" setup>

const selectedNum = inject('selectedNum')
const totalNum = inject('totalNum')
const operSwitch = inject('operSwitch',true)
const checkedAll = inject('checkedAll')

const finished=ref(true)
const loading=ref(false)
const error=ref(false)

//computed
const bottomPadding = computed(()=>{
	if(!operSwitch.value){
		return 'padding-bottom:calc(env(safe-area-inset-bottom))!important'
	}else{
		return 'padding-bottom:calc(50px + env(safe-area-inset-bottom))!important'
	}
})

//data
//收藏商品列表
const goodsList = ref(<Array<any>>[])
goodsList.value = [
	{
		gdsId:'1254260',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生 小象智能家庭医生小象智能家庭医生',
		agentPrice:43000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1227861',
		gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
		gdsName:'Apple iPhone 13 Pro Max',
		agentPrice:530000,
		gdsStatus:'0', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1227862',
		gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
		gdsName:'Apple iPhone 13 Pro Max Apple iPhone 13 Pro Max Apple iPhone 13 Pro Max',
		agentPrice:530000,
		gdsStatus:'2', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1227863',
		gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
		gdsName:'Apple iPhone 13 Pro Max',
		agentPrice:530000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
		oldPrice:630000,
	},
	{
		gdsId:'1254264',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生 小象智能家庭医生 小象智能家庭医生',
		agentPrice:43000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
		oldPrice:425000,
	},
	{
		gdsId:'1254265',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生',
		agentPrice:43000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1254266',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生 小象智能家庭医生小象智能家庭医生',
		agentPrice:43000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1227867',
		gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
		gdsName:'Apple iPhone 13 Pro Max',
		agentPrice:530000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1227868',
		gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
		gdsName:'Apple iPhone 13 Pro Max Apple iPhone 13 Pro Max Apple iPhone 13 Pro Max',
		agentPrice:530000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1227869',
		gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
		gdsName:'Apple iPhone 13 Pro Max',
		agentPrice:530000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1254270',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生 小象智能家庭医生 小象智能家庭医生',
		agentPrice:43000,
		guidePrice:48000,
		shopId:'ABRL4321',
		shopType:'厂家自营店',
	},
	{
		gdsId:'1254271',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生',
		agentPrice:43000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1254272',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生 小象智能家庭医生小象智能家庭医生',
		agentPrice:43000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1227873',
		gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
		gdsName:'Apple iPhone 13 Pro Max',
		agentPrice:530000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1254274',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生 小象智能家庭医生 小象智能家庭医生',
		agentPrice:43000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
	{
		gdsId:'1254275',
		gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
		gdsName:'小象智能家庭医生',
		agentPrice:43000,
		gdsStatus:'1', //1有效 0已下架 2无货
		shopId:'ABRL4321',
	},
]

//先假设已经读取了后台数据，返回了totalNum (后面要改的)
totalNum.value=goodsList.value.length

//methods
const checkDentity=()=>{
	let allCheck=true,selNum=0
	for(let tmp of goodsList.value){
		if(!tmp.checked){
			allCheck=false
		}else{
			++selNum
		}
	}
	selectedNum.value=selNum
	checkedAll.value=allCheck
}

const toggleAll=(val:boolean)=>{
	let selNum=0
	for(let tmp of goodsList.value){
		tmp.checked=val
		if(tmp.checked)	++selNum
	}
	selectedNum.value=selNum
	checkedAll.value=val
}
const deleteBatch=()=>{
	let len = 0, delList:string[]=[]
	for(let item of goodsList.value){
		if(item.checked){
			++len
			delList.push(item.gdsId)
		}
	}
	console.log(delList)
	return delList
}
defineExpose({
	toggleAll, deleteBatch,
})
</script>